package virtualTabletop;

import java.awt.Color;
import java.awt.Frame;
import java.awt.TextArea;

import ucigame.Sprite;
import ucigame.Ucigame;
import character.Character;

/**
 * This class does everthing that has to do with anything graphical.
 * 
 * 
 */
/*
 * In Run Configurations set width to atleast 551 and height to atleast 571.
 */
public class VirtualMap extends Ucigame
{

	/**
	 *
	 */
	private static final long serialVersionUID = -4620240589117708671L;

	Sprite sprite;
	VirtualTabletop tabletop;

	private Frame combatInfo = new Frame("Combat info");
	private TextArea combat = new TextArea();

	private Frame characterInfo = new Frame("Character info");
	private TextArea character = new TextArea();

	@Override
	public void setup()
	{

		combatInfo.setSize(551, 200);
		combatInfo.setVisible(true);
		combatInfo.setBackground(Color.white);
		combatInfo.add(combat);

		characterInfo.setSize(200, 700);
		characterInfo.setVisible(true);
		characterInfo.setBackground(Color.white);
		characterInfo.add(character);

		keyboard.typematicOff();
		window.size(551, 700);
		window.title("Virtual Map");
		canvas.background(getImage("virtualTabletop/gfx/grid_med_ramme2.png"));

		tabletop = new VirtualTabletop();

		character.append(tabletop.getTURN().toString());
	}

	@Override
	public void draw()
	{
		canvas.clear();

		for (Character c : tabletop.getRoundlist())
		{
			if (c.isAlive())
			{
				sprite = makeSprite(getImage(c.getSprite(), 255, 255, 255));
				;
				sprite.position(xToPixel(c.getX()), yToPixel(c.getY()));
				sprite.draw();
			}
		}
	}

	/**
	 * A way to calculate the x to a pixel count.
	 * 
	 * @param x
	 *            x coord
	 * @return pixel
	 */
	private int xToPixel(int x)
	{
		return Setup.pxSize * x + x + 1 + Setup.margin;
	}

	/**
	 * A way to calculate the x to a pixel count.
	 * 
	 * @param y
	 *            y coord
	 * @return pixel
	 */
	private int yToPixel(int y)
	{
		return Setup.pxSize * y + y + 1 + Setup.margin_top;
	}

	@Override
	public void onKeyPress()
	{
		if (keyboard.isDown(keyboard.F))
		{
			combat.append(tabletop.attack(tabletop.getTURN().getTarget()));
			combat.append(tabletop.winCheck());
			combat.repaint();
		}
		if (keyboard.isDown(keyboard.K1, keyboard.Z))
		{
			tabletop.move(Direction.SW);
		}
		if (keyboard.isDown(keyboard.K2, keyboard.X, keyboard.DOWN))
		{
			tabletop.move(Direction.S);
		}
		if (keyboard.isDown(keyboard.K3, keyboard.C))
		{
			tabletop.move(Direction.SE);
		}
		if (keyboard.isDown(keyboard.K4, keyboard.A, keyboard.LEFT))
		{
			tabletop.move(Direction.W);
		}
		if (keyboard.isDown(keyboard.K5, keyboard.S))
		{
			tabletop.move(Direction.C);
		}
		if (keyboard.isDown(keyboard.K6, keyboard.D, keyboard.RIGHT))
		{
			tabletop.move(Direction.E);
		}
		if (keyboard.isDown(keyboard.K7, keyboard.Q))
		{
			tabletop.move(Direction.NW);
		}
		if (keyboard.isDown(keyboard.K8, keyboard.W, keyboard.UP))
		{
			tabletop.move(Direction.N);
		}
		if (keyboard.isDown(keyboard.K9, keyboard.E))
		{
			tabletop.move(Direction.NE);
		}
		if (keyboard.isDown(keyboard.N))
		{
			tabletop.nextTurn();
			character.setText(tabletop.getTURN().toString());
			character.repaint();
		}
	}

	@Override
	public void onMousePressed()
	{
		int x = mouseXtoX(mouse.x());
		int y = mouseYtoY(mouse.y());
		if (x == -1 && tabletop.getTURN().getX() > 0)
		{
			x = 0;
		}
		else if (x >= Setup.width
				&& tabletop.getTURN().getX() < Setup.width - 1)
		{
			x = 9;
		}
		if (y == -1 && tabletop.getTURN().getY() > 0)
		{
			y = 0;
		}
		else if (y >= Setup.hight
				&& tabletop.getTURN().getY() < Setup.hight - 1)
		{
			y = 9;
		}

		tabletop.walkTowards(x, y);
	}

	/**
	 * calculates the mouse pixelcount to x.
	 * 
	 * @param x
	 *            pixel mouse is at
	 * @return x-coord.
	 */
	private int mouseXtoX(int x)
	{
		if (x < Setup.margin)
		{
			return -1;
		}
		else
		{
			return (x - (Setup.margin + 1)) / Setup.pxSize;
		}
	}

	/**
	 * calculates the mouse pixelcount to y.
	 * 
	 * @param y
	 *            pixel mouse is at
	 * @return y-coord.
	 */
	private int mouseYtoY(int y)
	{
		if (y < Setup.margin_top)
		{
			return -1;
		}
		else
		{
			return (y - (Setup.margin_top + 1)) / Setup.pxSize;
		}
	}
}
